<template>
  <div class="yy-inspection-tasks">
    <!--列表-->
    <div class="yy-goods-manage-list" v-show="type=='list'">
      <Card>
        <p slot="title">
          <Icon type="md-list" />
          巡检任务列表
        </p>
        <Form ref="formInline" :model="formSearch"  inline>
          <FormItem prop="user">
            <Input type="text" v-model="formSearch.ditName" placeholder="巡检任务名称"></Input>
          </FormItem>
          <FormItem prop="user">
            <Input type="text" v-model="formSearch.ditName" placeholder="巡检任务编码"></Input>
          </FormItem>
          <FormItem prop="user">
            <DatePicker type="date" placeholder="起始时间" style="width: 200px" v-model="formSearch.ditSt" :options="startTimeOption" @on-change="onStartTimeChange"></DatePicker>
          </FormItem>
          <FormItem prop="user">
            <DatePicker type="date" placeholder="结束时间" style="width: 200px" v-model="formSearch.ditEt" :options="endTimeOption" @on-change="onEndTimeChange"></DatePicker>
          </FormItem>
          <FormItem>
            <Button type="primary" @click="handleSearch()">查询</Button>
          </FormItem>
          <FormItem>
            <Button type="primary" @click="handleAdd()">新增</Button>
          </FormItem>
        </Form>
        <Divider> 巡检任务 </Divider>
        <div class="qrcode-list" align="center">
          <ul>
            <li class="qrcode-list-li" v-for="(item,index) in data" :key="index">
              <Card class="card">
                <p slot="title">
                    <Icon type="ios-book-outline" />
                    基本信息
                </p>
                <ul class="qrcode-list-card-up">
                  <li class="qrcode-list-card-length"><label>巡检计划名称：</label>{{item.dipName}}</li>
                  <li><label>巡检计划编号：</label>{{item.dipNum}}</li>
                  <li><label>巡检部门：</label>{{item.keShi}}</li>
                  <li><label>已下发：</label>{{item.fenpei}}</li>
                  <li><label>待下发：</label>{{item.notfenpei}}</li>
                  <li><label>状态：</label>{{item.isActive ? "启用" : "停用"}}</li>
                </ul>
                <div class="qrcode-list-card-down">
                  <Form inline>                    
                    <FormItem>
                      <Button type="primary" @click="details(index)">查看详情</Button>
                    </FormItem>
                    <FormItem>
                      <Button type="primary" @click="issue(index)">下发</Button>
                    </FormItem>
                  </Form>
                </div>
              </Card>
            </li>
          </ul>
        </div>
        <!--<Table :columns="columns" :data="data" ></Table>-->
        <div style="margin: 10px;overflow: hidden">
          <div style="float: right;">
              <Page :total="page.total" :page-size="page.pageSize" :current="1" @on-change="changePage" show-total></Page>
          </div>
        </div>
      </Card>
    </div>
    <!--详情-->
    <div class="yy-goods-manage-details" v-show="type=='details'">
       <Card>
        <p slot="title">
          <Icon type="md-paper"/>
          查看详情
        </p>
        <p slot="extra">
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <div class="details">
          <p class="details-title"><span>物品编号:</span>{{currentData.itemNum}}</p>
        </div>
        <Table :columns="columnsA" :data="dataA" @on-selection-change="https1"></Table>
      </Card>
    </div>
    <!--添加/编辑-->
    <div class="yy-goods-manage-add-edit"  v-show="type=='edit'">
      <Card>
        <p slot="title">
          <template v-if="type=='edit'"> 
            <Icon type="md-document"/>下发
          </template>
        </p>
        <p slot="extra" >
          <Button type="primary" @click="goBack()">返回</Button>
        </p>
        <div class="">
          <Form ref="editForm" :model="editFormInLine" :rules="editFormRuleInline" @keydown.native.enter.prevent ="handleSubmit" inline> 
            <FormItem prop="CateNum" placeholder="请选择分类名称" >
              <Select class="select" v-model="editFormInLine.CateNum"  placeholder="请选择分类名称">
                <Option value="">请选择</Option>
              </Select>
            </FormItem>
            <FormItem prop="CateNum" placeholder="请选择分类名称" >
              <Select class="select" v-model="editFormInLine.CateNum"  placeholder="请选择分类名称">
                <Option value="">请选择</Option>
              </Select>
            </FormItem>      
            <FormItem>
              <Button @click="handleSubmit" type="primary" class="button">提交</Button>      
            </FormItem>
            <FormItem>
              <Button @click="handleReset" type="primary" class="button">重置</Button>
            </FormItem>
          </Form>
          <Table :columns="columnsA" :data="dataA" @on-selection-change="https2"></Table>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import API from "./api";
import ValidatorRule from "@/filters/validator-rule";
import { formatNetTime } from "@/libs/tools";
import i18n from "./locale";
import mixin from "@/mixin/date-mixin";

export default {
  name: "yy-inspection-tasks",
  mixins: [mixin],
  data() {
    return {
      i18n: i18n,
      currentData: "",
      detailsModal: false,
      type: "list",
      dataCateName: [],
      page: {
        total: 0,
        pageSize: 20
      },
      formSearch: {
        dipNum: "",
        dipName: "",
        ditSt: "",
        ditEt: ""
      },
      editFormInLine: {
        CateNum: "",
        CateName: "",
        ItemNum: "",
        ItemName: "",
        MinNum: "",
        MaxNum: "",
        UnitName: "",
        isSpare: 0,
        isFX: 0,
        Size: "",
        Color: "",
        CusName: "",
        Description: "",
        remark: "",
        userID: "",
        userName: "",
        b_UserRole: "",
        brand: ""
      },
      editFormRuleInline: {
        ItemNum: ValidatorRule.require,
        ItemName: ValidatorRule.require,
        CateNum: ValidatorRule.select
      },
      columns: [
        {
          title: "巡检计划名称",
          key: "dipName"
        },
        {
          title: "巡检计划编码",
          key: "dipNum"
        },
        {
          title: "巡检部门 ",
          key: "keShi"
        },
        {
          title: "已下发 ",
          key: "fenpei"
        },
        {
          title: "待下发 ",
          key: "notfenpei"
        },
        {
          title: "操作",
          key: "action",
          width: 180,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.details(params.index);
                    }
                  }
                },
                "查看"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.index);
                    }
                  }
                },
                "下发"
              )
            ]);
          }
        }
      ],
      data: [],
      columnsA: [
        {
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          title: "Name",
          key: "name"
        },
        {
          title: "Age",
          key: "age"
        },
        {
          title: "Address",
          key: "address"
        }
      ],
      dataA: []
    };
  },

  mounted() {
    this.userInfo = this.$store.state.user.userInfo;
    this.getCateList();
    this.handleSearch();
  },

  computed: {},

  methods: {
    getCateList() {
      let obj = {
        userID: "",
        userName: "",
        b_UserRole: "",
        hasLocation: 1
      };
      this.getCateNameList(obj);
    },

    getCateNameList(obj) {
      API.getCateNameList(obj)
        .then(res => {
          if (res.code == 200) {
            let dataCate = res.data;
            this.dataCateName = dataCate.list;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    handleSearch() {
      let obj = this.formSearch;
      obj.pageNum = 1;
      obj.pageSize = this.page.pageSize;
      this.formSearch = obj;
      this.getList(obj);
    },

    changePage(pageNum) {
      let obj = this.formSearch,
        maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      if (obj.pageNum > maxPageNum) {
        obj.pageNum = maxPageNum;
      } else {
        obj.pageNum = pageNum;
      }
      this.formSearch = obj;
      this.getList(obj);
    },

    getList(obj) {
      API.getList(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            if (data != null) {
              this.page.total = data.total;
              this.page.pageSize = data.pageSize;
              this.data = data.list;
            } else {
              this.$Message.success({ content: "暂无数据！" });
              this.data = [];
            }
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    goBack() {
      this.type = "list";
    },

    handleAdd() {
      this.type = "add";
      this.handleReset();
    },

    details(index) {
      this.type = "details";
      this.currentData = _.clone(this.data[index]);
      if (this.currentData.isSpare == 0) {
        this.currentData.isSpare = "否";
      } else {
        this.currentData.isSpare = "是";
      }
      if (this.currentData.isFX == 0) {
        this.currentData.isFX = "否";
      } else {
        this.currentData.isFX = "是";
      }
    },

    issue(index) {
      this.type = "edit";
      this.handleReset();
      this.currentData = this.data[index];
      //this.editFormInLine.cateNum = this.currentData.cateNum;
      //this.editFormInLine = _.clone(this.currentData);
    },

    delete(index, id) {
      let tthis = this;
      this.$Modal.confirm({
        title: "提示：",
        content: "你确定删除这条记录",
        onOk: function() {
          let obj = {
            ItemNum: "",
            ItemName: "",
            userID: tthis.userInfo.userId,
            userName: tthis.userInfo.userName,
            b_UserRole: ""
          };
          API.delete(obj)
            .then(res => {
              if (res.code == 200) {
                this.$Message.success({ content: "该记录已经删除成功！" });
                tthis.data.splice(index, 1);
                tthis.resetList();
              }
            })
            .catch(err => {
              console.log(err);
              this.$Message.error("请求异常！");
            });
        }
      });
    },

    resetList() {
      //求余为1，页码为最大页码，请求页需减1
      let maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      let remainderNum = this.page.total % this.page.pageSize;
      if (
        this.formSearch.pageNum == maxPageNum &&
        remainderNum == 1 &&
        this.formSearch.pageNum !== 1
      ) {
        this.formSearch.pageNum = this.formSearch.pageNum - 1;
      }
      let obj = this.formSearch;
      this.getList(obj);
    },

    handleSubmit() {
      //console.log(384, this.editFormInLine);
      this.$refs.editForm.validate(valid => {
        if (valid) {
          if (this.type == "edit") {
            this.editSubmit(this.editFormInLine);
          } else {
            this.addSubmit(this.editFormInLine);
          }
        }
      });
    },

    handleReset() {
      this.$refs.editForm.resetFields();
      if (this.type == "edit") {
        this.editFormInLine.ItemNum = this.currentData.itemNum;
        this.editFormInLine.ItemName = this.currentData.itemName;
        this.editFormInLine.CateNum = this.currentData.cateNum;
      }
    },

    editSubmit(obj) {
      this.formSearch.cateNum = obj.cateNum;
      //obj.id = this.currentData.id;
      API.editSubmit(obj)
        .then(res => {
          if (res.code == 200) {
            this.resetList();
            this.$Message.success("修改成功！");
            this.goBack();
          }
        })
        .catch(err => {
          this.$Message.error("修改异常！");
        });
    },

    addSubmit(obj) {
      API.addSubmit(obj)
        .then(res => {
          if (res.code == 200) {
            this.handleSearch();
            this.$Message.success("添加成功！");
            this.goBack();
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    }
  }
};
</script>
<style lang="less" >
@import "./yy-inspection-tasks.less";
</style>
